package com.sl.core.workflow;

import cn.hutool.core.date.DateUtil;
import com.sl.core.engine.ProcessConfig;
import com.sl.core.engine.ProcessEngine;
import com.sl.core.workflow.listener.WorkflowPipelineListener;

/**
 * todo workflow 待实现
 */
public class WorkFlowEngine {

    public static final WorkFlowEngine INSTANCE = new WorkFlowEngine();


    public void init() {
//        ProcessLoggingEventListener processLoggingEventListener = new ProcessLoggingEventListener();
//        HopLogStore.getAppender().addLoggingEventListener(processLoggingEventListener);
    }

    public Object start(ProcessConfig processConfig, String param) {

        //运行， 记录日志 改变状态
        processConfig.setStartTime(DateUtil.date());

        Object execute = ProcessEngine.INSTANCE.execute(param, processConfig, null, false, (t, p) -> {
            WorkFlowInstance workFlowInstance = new WorkFlowInstance(t, p);
            workFlowInstance.prepareExecution();
            workFlowInstance.setPrintStepStatusFlag(true);
            workFlowInstance.addExecutionFinishedListener(WorkflowPipelineListener.INSTANCE);

            return workFlowInstance;
        });
        return execute;
        //todo  执行到用户步骤要中断
        //并且要记录日志 和改变步骤
    }

    public Object next(ProcessConfig processConfig, String param) {
        return start(processConfig, param);
    }
}
